package cn.hxsteel.storage.core.tool;

import java.util.ArrayList;
import java.util.List;

import cn.hxsteel.storage.model.bo.Depot1BO;
import cn.hxsteel.storage.model.po.DepotPO;
import cn.hxsteel.storage.model.vo.DepotVO;

public class DepotTool
{
    /**
     * 取得仓库信息集合
     * 
     * @param depots 仓库数据
     */
    public static List< Depot1BO > giveDepotGroup( List< DepotPO > depots )
    {
	List< Depot1BO > depotList = new ArrayList< Depot1BO >();
	List< Short > ids = new ArrayList< Short >();
	List< String > keys = new ArrayList< String >();
	
	for ( DepotPO depotPO : depots )
	{
	    if ( depotPO.getPid() == 0 )
	    {
		ids.add( depotPO.getId() );
		keys.add( depotPO.getName() );
	    }
	}
	
	int length = ids.size();
	for ( int i = 0; i < length; i++ )
	{
	    Depot1BO depotBO = new Depot1BO();
	    depotBO.setName( keys.get( i ) );
	    depotBO.setDepots( giveChildDepotList( depots, ids.get( i ) ) );
	    depotList.add( depotBO );
	}
	
	return depotList;
    }
    
    /**
     * 返回二级仓库列表
     * 
     * @param depots
     */
    public static List< DepotVO > giveDepotList( List< DepotPO > depots )
    {
	List< DepotVO > depotList = new ArrayList< DepotVO >();
	depotList.add( new DepotVO( 0, "请选择！" ) );
	
	for ( DepotPO depotPO : depots )
	{
	    if ( depotPO.getPid() != 0 )
	    {
		DepotVO depotVO = new DepotVO();
		depotVO.setDepotID( depotPO.getId() );
		depotVO.setName( depotPO.getName() );
		depotList.add( depotVO );
	    }
	}
	
	return depotList;
    }
    
    /**
     * 根据仓库的父级ID返回该仓库的所有子仓库列表
     * 
     * @param depots 仓库数据
     * @param id 要返回子仓库列表的仓库的父级ID
     */
    private static List< DepotPO > giveChildDepotList( List< DepotPO > depots, int id )
    {
	List< DepotPO > depotList = new ArrayList< DepotPO >();
	for ( DepotPO depotPO : depots )
	{
	    if ( depotPO.getPid() == id )
	    {
		depotList.add( depotPO );
	    }
	}
	return depotList;
    }
}
